import yaml
import logging
import urllib, urllib2
import simplejson
from google.appengine.api import urlfetch
class YahooApi:
    config = None
    appid = None
    output = "json"
    def __init__(self):
        self.config = yaml.load(open('config.yaml'))
        self.appid = self.config["ydn_api_appid"]

class YahooKeywordApi(YahooApi):
    apiurl = "http://jlp.yahooapis.jp/KeyphraseService/V1/extract?"
    def get_keywords(self, text):
        form_fields = { "appid" :  self.appid, "output" : self.output, "sentence" : text.encode("utf-8")} 
        form_data = urllib.urlencode(form_fields)
        result = urlfetch.fetch( 
                    url = self.apiurl + form_data,
                    method = urlfetch.GET) 
        json = simplejson.loads(result.content)
        keywords = []
        for j in json:
            keywords.append(j)

        logging.info(keywords)
        return ",".join(keywords)

class YahooParseApi:
    apiurl = "http://jlp.yahooapis.jp/KeyphraseService/V1/extract?"
    def get_words(self, text):
        form_fields = { 
            "appid" :  self.appid, "output" : self.output, 
            "results" : "ma",
            "sentence" : text.encode("utf-8")} 
        form_data = urllib.urlencode(form_fields)
        result = urlfetch.fetch( 
                    url = self.apiurl + form_data,
                    method = urlfetch.GET) 
        return result.content


        
    
